package com.huitone.gddw.service.impl.monitoring.base;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.entity.Trigger;
import com.huitone.gddw.dao.monitoring.ITriggerDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.dto.monitoring.base.TriggerDTO;
import com.huitone.gddw.service.monitoring.base.IInnormalEventService;
import com.huitone.gddw.service.monitoring.base.ITriggerService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 监控预警触发 接口实现类
 * @author wuluodan
 *
 */
@Service
public class TriggerService implements ITriggerService {

	@Autowired
	private ITriggerDao triggerDao;
	
	@Autowired
	private IInnormalEventService innormalEventService;
	
	@Override
	public QueryPage<TriggerDTO> selectPage(QueryPage<Trigger> page) {
		Map<String, Object> params = page.getParams();
		params.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		params.put("end", page.getPageNo() * page.getPageSize());
		
		List<TriggerDTO> result = triggerDao.selectPage(params);
		int totalCount = triggerDao.selectCount(params);
		
		QueryPage<TriggerDTO> queryPage = new QueryPage<TriggerDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}
	
	@Override
	public Trigger getById(String triggerId) {
		return triggerDao.getById(triggerId);
	}

	@Override
	public TriggerDTO findOne(String triggerId) {
		return triggerDao.findOne(triggerId);
	}
	
	@Override
	public void save(Trigger entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		triggerDao.save(entity);
	}

	@Override
	public void update(Trigger entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		triggerDao.update(entity);
	}
	
	@Override
	public void delete(String triggerId) {
		// 监控异常事件信息
		innormalEventService.delByTriggerId(triggerId);
		
		triggerDao.delByTriggerId(triggerId);
	}

	@Override
	public void delByObjId(String objId) {
		triggerDao.delByObjId(objId);
	}

	@Override
	public void delByItemId(String itemId) {
		triggerDao.delByItemId(itemId);
	}

	@Override
	public void delByKeyId(String keyId) {
		triggerDao.delByKeyId(keyId);
	}

}
